package com.ssm.forum.rpc.service.impl.residence;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.forum.dao.mapper.residence.ResidenceDao;
import com.ssm.forum.dao.model.residence.Residence;
import com.ssm.forum.rpc.api.residence.ResidenceService;
import com.ssm.upms.dao.mapper.ssm.AccountDao;
import com.ssm.upms.dao.mapper.ssm.AccountRoleDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* 小区管理 Service
* @author GaoXiang
* @version 1.0
*/
@Service
public class ResidenceServiceImpl implements ResidenceService {

    @Resource
    private ResidenceDao residenceDao;
    @Resource
    private AccountDao accountDao;
    @Resource
    private AccountRoleDao accountRoleDao;

    /**
    * 保存数据
    * @param residence 实体对象
    * @return 实体id
    * @throws Exception 数据保存异常
    */
    public Integer insert(Residence residence) throws Exception {
        return residenceDao.insert(residence);
    }

    /**
    * 根据 id 修改
    * @param residence 带id的实体对象
    * @return 受影响的行数
    * @throws Exception 数据修改异常
    */
    public Integer updateById(Residence residence) throws Exception {
        return residenceDao.updateById(residence);
    }

    /**
    * 根据 id 删除
    * @param id 数据id
    * @return 受影响的行数
    * @throws Exception 数据删除异常
    */

    public Integer deleteById(String id) throws Exception {
     /*   List<Account> accounts = accountDao.findByResidenceId(id);
        if(accounts!=null && !accounts.isEmpty()){
            for (Account account:accounts) {
                AccountRole accountRole = accountRoleDao.selectByAccountId(account.getId());
                if(accountRole!=null){
                    accountRoleDao.deleteById(accountRole.getId());//删除关联表信息
                }

            }
        }
        if(accounts!=null && !accounts.isEmpty()){
            for (Account account:accounts) {
            accountDao.deleteById(account.getId());//删除account表信息
            }
        }*/
        return residenceDao.deleteById(id);
    }


    /**
    * 根据 id 查找
    * @param id 实体id
    * @return 实体
    */
    public Residence selectById(String id) {
        return residenceDao.selectById(id);
    }

    /**
    * 根据 id 批量删除
    * @param ids 要删除的id
    * @return 影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteByIds(List<String> ids) throws Exception {
        return residenceDao.deleteByIds(ids);
    }

    /**
    * 查询列表
    * @param map 参数
    * @return 列表
    */
    public PageInfo<Residence> list(Map map) {
        return residenceDao.list(map);
    }

    /**
    * 查询列表 带分页
    * @param map 参数
    * @param page 页码
    * @param size 每页大小
    * @return 列表
    */
    public PageInfo<Residence> list(Map map,int page,int size) {
        return residenceDao.list(map,page,size);
    }

    public Long countCode(Map map){
        return  residenceDao.countCode(map);
    }

    public List<Residence> listAll(Residence residence) {
        return residenceDao.listAll(residence);
    }

    public List<Residence> countName(Residence residence) {
        return residenceDao.countName(residence);
    }

    public Residence countNumber(Residence residence) {
        return residenceDao.countNumber(residence);
    }

    public List<Residence> countName2(Residence residence) {
        return residenceDao.countName2(residence);
    }

    public Residence countNumber2(Residence residence) {
        return residenceDao.countNumber2(residence);
    }

    public List<Residence> chooseResidence(Map map){ return  residenceDao.chooseResidence(map);}



    public List<Residence> findName(Residence residence) {
        return residenceDao.findName(residence);
    }

    public List<Residence> listByEstateId(String estateId){
        return residenceDao.listByEstateId(estateId);
    }

    public List<Residence> listByPoliceId(String policeId){
        return residenceDao.listByPoliceId(policeId);
    }


    public List<Residence> findResidenceByCity(String cityName){
        return residenceDao.findResidenceByCity(cityName);
    }
    public List<Residence> findResidenceByArea(String areaName){
        return residenceDao.findResidenceByArea(areaName);
    }

    public PageInfo<Residence> resAdminList(String id,int page,int size){ return  residenceDao.resAdminList(id,page,size);}

    /**
     * 根据police_id查询
     * @param map
     * @return
     */
    public List<Residence> findByPoliceId(Map map){return residenceDao.findByPoliceId(map);}

    public List<Residence> listWithoutPage(ParamMap paramMap) {
        return residenceDao.listWithoutPage(paramMap);
    }

    public List<Residence> listByAccount(ParamMap p) {
        return residenceDao.listByAccount(p);
    }


    /**
     * 根据小区的编号查询
     * 2017-5-22 14:45:33 韩榕榕
     * @param map 参数
     * @return 实体
     */
    public Residence selectByresCode(Map map){return  residenceDao.selectByresCode(map);}
}
